//
//  UINavigationController+Extend.m
//  CedarFinancePH
//
//  Created by lshenrong on 2018/4/2.
//  Copyright © 2018年 cedarhd. All rights reserved.
//

#import "UINavigationController+Extend.h"

@implementation UINavigationController (Extend)


- (void)pushToNewViewController:(UIViewController *)vc{
    vc.hidesBottomBarWhenPushed = YES;
    [self pushViewController:vc animated:YES];
}

-(void)removeViewController:(NSString *)viewControllerName
{
    NSMutableArray * viewControllers = [self.viewControllers mutableCopy];
    for (UIViewController *viewController in [viewControllers reverseObjectEnumerator]) {
        NSString *tempName = NSStringFromClass([viewController class]);
        if ([tempName isEqualToString:viewControllerName]) {
            [viewControllers removeObject:viewController];
        }
    }
    [self setViewControllers:viewControllers animated:NO];
    
}

- (void)removeViewControllerWithArrayName:(NSArray *)nameArray{
    NSMutableArray * viewControllers = [self.viewControllers mutableCopy];
    for (UIViewController *viewController in [viewControllers reverseObjectEnumerator]) {
        NSString *tempName = NSStringFromClass([viewController class]);
        if ([nameArray containsObject:tempName]) {
            [viewControllers removeObject:viewController];
        }
    }
    [self setViewControllers:viewControllers animated:NO];
}

- (void)removeTheMidleViewControllers{
    NSArray *viewControllers = self.viewControllers;
    if (viewControllers.count > 2 ) {
        UIViewController *rootCtrl = [self.viewControllers firstObject];
        UIViewController *topCtrl = [self.viewControllers lastObject];
        NSArray *newArr = [NSArray arrayWithObjects:rootCtrl,topCtrl, nil];
        [self setViewControllers:newArr animated:NO];
    }
}

- (void)removeAllExceptViewController:(NSString *)vcName{
    NSMutableArray * viewControllers = [self.viewControllers mutableCopy];
    for (UIViewController *viewController in [viewControllers reverseObjectEnumerator]) {
        NSString *tempName = NSStringFromClass([viewController class]);
        if (![tempName isEqualToString:vcName]) {
            [viewControllers removeObject:viewController];
        }
    }
    [self setViewControllers:viewControllers animated:NO];
}


- (void)removeAllExceptFirstViewController{
    NSArray *viewControllers = self.viewControllers;
    
    if (viewControllers.count >= 2 ) {
        UIViewController *rootCtrl = [self.viewControllers firstObject];
        NSArray *newArr = [NSArray arrayWithObjects:rootCtrl, nil];
        [self setViewControllers:newArr animated:NO];
    }
    
}

- (BOOL)isEqualToPreviousControllerName:(NSString *)controlleName{
    NSArray *array =self.viewControllers;
    
     UIViewController *lastVC = [self.viewControllers lastObject];
    
    NSInteger index = [self.viewControllers indexOfObject:lastVC];
    index--;
    if (index>=0 && index< array.count) {
        UIViewController *vc = array[index];
        NSString *tempName = NSStringFromClass([vc class]);
        if ([controlleName isEqualToString:tempName]) {
            return YES;
        }
    }
    return NO;
}


- (BOOL)popToOldViewControllerName:(NSString *)controllerName{
    BOOL isSucess = NO;
    for (UIViewController *controller in self.viewControllers) {
        NSString *tempName = NSStringFromClass([controller class]);
        if ([tempName isEqualToString:controllerName]) {
            [self popToViewController:controller animated:YES]; 
            isSucess = YES;
            break;
        }
    }
    return YES;
}
- (BOOL)popToOldViewController:(UIViewController *)vc{
    BOOL isSucess = NO;
    for (UIViewController *controller in self.viewControllers) {
        NSString *tempName = NSStringFromClass([controller class]);
        NSString *tempName2 = NSStringFromClass([vc class]);
        if ([tempName isEqualToString:tempName2]) {
            [self popToViewController:controller animated:YES];
            isSucess = YES;
            break;
        }
    }
    return YES;
}



- (void)showNavigationBarShadow:(BOOL)show{
    if (show) {
        [self.navigationBar setShadowImage:nil];
       // [self.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];

//         [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    }else{
         //[self.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
       
        [self.navigationBar setShadowImage:[[UIImage alloc] init]];
    }
}

//- (void)pushToWebViewController:(NSString *)webUrl{
//    if(!IsEmptyStr(webUrl)){
//        WebViewBaseViewController *webVC =  [[WebViewBaseViewController alloc]init];
//        webVC.webViewlURL = webUrl;
//        [self pushToNewViewController:webVC];
//    }
//}

@end
